package com.zhanqiao.Dao;

import com.zhanqiao.Utils.GetDataSourceUtil;
import com.zhanqiao.domain.ArticleBean;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class DraftDao implements ArticleDao {
    @Override
    public int insertArticle(ArticleBean articleBean) {
        return 0;
    }

    @Override
    public int deleteArticle(int articleId) {
        return 0;
    }

    @Override
    public List<ArticleBean> selectAll() {
        return null;
    }



    //查询到所有未发送的文章
    public List<ArticleBean> selectAll(String page, String limit) {
        int current = Integer.parseInt(page);
        int counts = Integer.parseInt(limit);
//        System.out.println("current="+current+"counts="+counts);
        DataSource ds = GetDataSourceUtil.getDataSource();
        QueryRunner queryRunner = new QueryRunner(ds);
        Object[] parm = {(current - 1) * counts, counts};
        try {
            List<List<ArticleBean>> list = queryRunner.execute("select * from article where sendstate='0' limit ?,?", new BeanListHandler<ArticleBean>(ArticleBean.class), parm);
            return list.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //草稿发送功能
    @Override
    public int updateArticle(int id, Date publishdate, String title, String content) {
        DataSource ds = GetDataSourceUtil.getDataSource();
        QueryRunner queryRunner = new QueryRunner(ds);
        Object[] pram = {title,publishdate,content,id};
        System.out.println(Arrays.toString(pram));
        String sql = "update article set title=?,publishdate=?,content=?,sendstate=1 where aid =?";
        try {
            int update = queryRunner.update(sql, pram);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

}
